package com.group4.blockchain.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.group4.blockchain.common.lang.Result;
import com.group4.blockchain.pojo.User;
import com.group4.blockchain.service.UserService;
import com.group4.blockchain.util.JwtUtils;
import com.group4.blockchain.util.RandomID;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
public class UserController {
    //Restful
    @Autowired
    UserService userService;

    @Autowired
    JwtUtils jwtUtils;

    @ApiOperation(value="用户登录请求",notes = "传入username,password")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(@RequestBody Map<String,String> map, HttpServletResponse response){
        String username = map.get("username");
        String password = map.get("password");

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = userService.getOne(wrapper);
        //如果没有
        Assert.notNull(user, "用户不存在");

        if(!user.getPassword().equals(password)){
            return Result.fail("密码不正确");
        }

        String jwt = jwtUtils.generateToken(user.getId());

        response.setHeader("Authorization", jwt);
        response.setHeader("Access-control-Expose-Headers", "Authorization");
//        ContractController.enrollAdmin();

        return Result.succ(user);
    }

    @ApiOperation(value="用户注册请求",notes = "传入String username,String password,String location,Integer role")
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public Result register(@RequestBody User loginUser){
    //username,password,location,role
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",loginUser.getUsername());
        User user = userService.getOne(wrapper);
        if(user != null ){
            return Result.fail("用户名已经存在！");
        }else{
            User new_user = new User();
            new_user.setId(RandomID.getUUID());
            new_user.setUsername(loginUser.getUsername());
            new_user.setPassword(loginUser.getPassword());
            new_user.setLocation(loginUser.getLocation());
            new_user.setRole(loginUser.getRole());
            new_user.setStatus(0);
            new_user.setIsReal(0);

            boolean judge = userService.save(new_user);
            if(judge){
                return Result.succ("用户注册成功");
            }else{
                return Result.fail("用户注册失败");
            }
        }
    }

    @ApiOperation(value="用户查询请求")
    @RequestMapping(value = "/getInfo/{id}",method = RequestMethod.GET)
    public Result getInfo(@PathVariable(name = "id") String id){

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        User user = userService.getOne(wrapper);
        if(user != null){
            return Result.succ(200,"查询成功",user);
        }else{
            return Result.fail("查询失败");
        }
    }

//    @ApiOperation(value="用户修改请求")
//    @RequestMapping(value = "/updateInfo",method = RequestMethod.PUT)
//    public Result updateInfo(@RequestBody User userDto){
//
//        User user = userService.getOne(new QueryWrapper<User>().eq("id", userDto.getId()));
//        if (user==null)
//            return Result.fail("用户不存在");
//        user.setUsername(userDto.getUsername());
//        user.setLocation(userDto.getLocation());
//        user.setPassword(userDto.getPassword());
//        boolean judge = userService.updateById(user);
//        if(judge){
//            return Result.succ("用户信息修改成功");
//        }
//        else{
//            return Result.fail("用户信息修改失败");
//        }
//    }

    @ApiOperation(value="登出")
    @RequiresAuthentication
    @GetMapping("/logout")
    public Result logout() {
        SecurityUtils.getSubject().logout();
        return Result.succ(null);
    }

}
